package dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import pojo.Doctor;

import java.util.List;

/**
 * @author 李主席
 * @create 2020-12-11 8:52
 */
@Repository
public class DoctorDaoImpl implements DoctorDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Doctor> selectAllDoctors(Integer start, Integer length) {
//将数据库的一行映射到对象的属性上
        RowMapper<Doctor> rowMapper = new BeanPropertyRowMapper<>(Doctor.class);

        List<Doctor> doctors = jdbcTemplate.query("select * from doctor limit ?, ?", rowMapper, start, length);

        return doctors;
    }

    @Override
    public Integer selectDoctorsCount() {

        return jdbcTemplate.queryForObject("select  count(*) from doctor ", Integer.class);
    }

    @Override
    public Integer insertOneDoctor(Doctor doctor) {
        String sql = "insert into doctor(name,sex,birthday,age,did) values(?,?,?,?,?)";
        return jdbcTemplate.update(sql, doctor.getName(),doctor.getSex(),doctor.getBirthday(), doctor.getAge(), doctor.getDid());
    }

    @Override
    public Integer updateOneDoctor(Doctor doctor) {
        String sql = "update doctor set name=?,sex=?, birthday=?, age=? did=? swhere id=? ";

        return jdbcTemplate.update(sql, doctor.getName(), doctor.getSex(),doctor.getBirthday() ,doctor.getAge(), doctor.getDid(),doctor.getId());
    }

    @Override
    public Doctor selectDoctorById(Integer id) {
        RowMapper<Doctor> rowMapper = new BeanPropertyRowMapper<>(Doctor.class);

        return jdbcTemplate.queryForObject("select * from doctor where id = ?", rowMapper, id);
    }
    @Override
    public Integer deleteAllDoctors(Integer id) {
        return jdbcTemplate.update("delete from doctor where id = ?", id);
    }
}
